package cn.com.gridinfo.sooc.core.course.classes.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;


import com.jeremy.arad.utils.Log;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseDetailsInfo;
import cn.com.gridinfo.sooc.core.course.classes.dao.CourseDao;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 图片 fragment
 */
public class ImageFragment extends BaseSoocFragment {

    View view;
    @InjectView(R.id.course_img)
    WebView courseImgView; //图片播放区域
    @InjectView(R.id.img_contanier)
    FrameLayout imgContanier;//播放区域容器
    private String imgPath;//图片播放地址

    //课程详情 实体类
    CourseDetailsInfo courseDetailsInfo;

    //课程详情 dao
    CourseDao dao;

    //学习记录ID
    String studyId = "0" ;

    /**
     * 实例化图片Fragment
     *
     * @param path 图片路径
     * @return
     */
    public static ImageFragment newInstance(CourseDetailsInfo courseDetailsInfo, String path) {
        ImageFragment imgFragment = new ImageFragment();
        Bundle args = new Bundle();
        args.putString("imgPath", path);
        args.putSerializable("courseDetail",courseDetailsInfo);
        imgFragment.setArguments(args);
        return imgFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.course_image, null);
        ButterKnife.inject(this, view);
        //课程详情 dao
        dao = new CourseDao(this);
        //图片播放地址
        if (getArguments() != null) {
            imgPath = getArguments().getString("imgPath");
            //获取课程详情
            courseDetailsInfo = (CourseDetailsInfo)getArguments().getSerializable("courseDetail");
        }

        //初始化文档页面
        initImgWebView();

        return view;
    }

    // 初始化文档页面
    private void initImgWebView() {

        courseImgView.setClickable(true);
        courseImgView.getSettings().setJavaScriptEnabled(true);//允许脚本
        courseImgView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 隐藏缩放图片
        courseImgView.getSettings().setDisplayZoomControls(false);// 显示放大缩小
        courseImgView.getSettings().setSupportZoom(true);
        courseImgView.getSettings().setBuiltInZoomControls(true);
        // 默认缩放模式
        String imageHtml = "<!DOCTYPE html> <html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "</head>\n" +
                "<body >\n" +
                "<div ><img align=\"center\" width=\"100%\"  src=\""+imgPath+"\" style=\"vertical-align:middle;\" ></div>\n" +
                "</body>\n" +
                "</html>";
//
        courseImgView.getSettings().setUseWideViewPort(true);// 无限缩放
        courseImgView.getSettings().setLoadWithOverviewMode(true);//加载页面的到屏幕大小模式
        courseImgView.loadDataWithBaseURL("",imageHtml,"text/html","utf-8",null);
    }


    /**
     * 请求成功,请求成功
     */
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.status.equals("0")) {
                switch (requestCode) {
                    //记录学习行为
                    case CodeConst.COURSE_SETBEHAVIOR_START:
                        studyId = dao.studyId;//记录学习行为ID
                        break;

                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.statusCode);
            }

        } catch (Exception e) {
//            Log.e(e.getMessage(), e);
//            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        //记录当前播放内容开始学习行为,并且不是课程老师
        if (courseDetailsInfo.getKcls() == 0) {
            dao.setBehavior(courseDetailsInfo.getKcid(), courseDetailsInfo.getCurrPmId(), courseDetailsInfo.getCurrMlId(), courseDetailsInfo.getCurrNrId(), "0",0);
            Log.d("播放记录开始记录..."+courseDetailsInfo.getCurrNrId());
        }

    }

    @Override
    public void onPause() {
        super.onPause();

        //结束学习记录记录学习行为
        if (!studyId.equals("0")) {
            //并且不是课程老师
            if (courseDetailsInfo.getKcls() == 0) {
                dao.setBehavior(courseDetailsInfo.getKcid(), courseDetailsInfo.getCurrPmId(), courseDetailsInfo.getCurrMlId(), courseDetailsInfo.getCurrNrId(), studyId, 0);
                Log.d("播放记录记录停止..."+courseDetailsInfo.getCurrNrId());
            }
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
